package ua.kharkov.khpi.tarasenko.bach.web.command.admin;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ua.kharkov.khpi.tarasenko.bach.core.Constant;
import ua.kharkov.khpi.tarasenko.bach.core.EShopException;
import ua.kharkov.khpi.tarasenko.bach.core.db.ConnectionManager;
import ua.kharkov.khpi.tarasenko.bach.core.db.DBUtils;
import ua.kharkov.khpi.tarasenko.bach.dao.core.assocrules.AssocRulesDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.mysql.assocrules.AssocRulesDAOImpl;
import ua.kharkov.khpi.tarasenko.bach.service.AnalysisService;
import ua.kharkov.khpi.tarasenko.bach.web.command.Command;

public class ShowRecommendationsSettings extends Command {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws EShopException {

		String forward = Constant.PAGE_RECOMMENDATIONS_SETTINGS;

		int numOfRules = getNumberOfRulesInDb();
		boolean isTreeInDB = isTreeInDB();
		
		request.setAttribute("isTreeInDb", isTreeInDB);
		request.setAttribute("numOfRules", numOfRules);
		
		return forward;
	}

	private int getNumberOfRulesInDb() throws EShopException {
		Connection conn = ConnectionManager.getConnection();

		AssocRulesDAO rulesDAO = new AssocRulesDAOImpl(conn);
		int result = rulesDAO.getNumberOfRules();
		DBUtils.close(conn);
		return result;
	}
	
	private boolean isTreeInDB() throws EShopException {
		AnalysisService serv = new AnalysisService();
		return serv.isTreeInDb();
	}
}
